package com.persondemo.videoappliction.ui.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.persondemo.videoappliction.R;
import com.persondemo.videoappliction.bean.TvMovieBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/3/28.
 */

public class MoiveTypeAdapter extends RecyclerView.Adapter<MoiveTypeAdapter.SimpleHolder> {
    private List<TvMovieBean.DataBean.TypesListBean> list;
    Context context;
    private LayoutInflater mInflater;
    private OnItemActionListener mOnItemActionListener;
    private List<Boolean> isClicks;//控件是否被点击,默认为false，如果被点击，改变值，控件根据值改变自身颜色

    public MoiveTypeAdapter(List<TvMovieBean.DataBean.TypesListBean> list, Context context) {
        this.list = list;
        this.context = context;
        mInflater = LayoutInflater.from(context);
        isClicks = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            isClicks.add(false);
        }
    }

    @Override
    public SimpleHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = mInflater.inflate(R.layout.module_pop_movie, null, false);
        SimpleHolder holder = new SimpleHolder(v);
        holder.setIsRecyclable(true);
        return holder;
    }

    @Override
    public void onBindViewHolder(final MoiveTypeAdapter.SimpleHolder holder, final int position) {
        holder.name.setText(list.get(position).getTypes_name());
        if (isClicks.get(position)) {
            holder.name.setTextColor(Color.parseColor("#F68E54"));
        } else {
            holder.name.setTextColor(Color.parseColor("#000000"));
        }
        if (mOnItemActionListener != null) {
            holder.name.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemActionListener.onItemClickListener(v, holder.getPosition());
                    for (int i = 0; i < isClicks.size(); i++) {
                        isClicks.set(i, false);
                    }
                    isClicks.set(position, true);
                    notifyDataSetChanged();
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public interface OnItemActionListener {
        void onItemClickListener(View v, int pos);
    }

    public void setOnItemActionListener(OnItemActionListener onItemActionListener) {
        this.mOnItemActionListener = onItemActionListener;
    }

    public class SimpleHolder extends RecyclerView.ViewHolder {
        public TextView name;

        public SimpleHolder(View itemView) {
            super(itemView);
            name = (TextView) itemView.findViewById(R.id.card_title_tv);
        }
    }
}
